package com.oa.tools.jobs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Node;

import com.oa.tools.TkMapper;
import com.oa.tools.entity.OAFlowInstanceWithBLOBs;
import com.oa.tools.entity.Person;
import com.oa.tools.entity.Stamp;
import com.oa.tools.jobs.FlowHandlerTaskBase;
import com.oa.tools.utils.XmlUtil;
import com.oa.tools.utils.XmlUtil.XMLHandler;

import tk.mybatis.mapper.entity.Example;

public class UpdateStampKeeperTask extends FlowHandlerTaskBase{

	public UpdateStampKeeperTask(Map<String,TkMapper<?>> daoMap) {
		super(daoMap);
		// TODO Auto-generated constructor stub
		this.flowStatus = FlowHandlerTaskBase.FLOW_STATUS_DONE;
		this.templateId = "15ec7a07af3ce3a9de0c8b94764951d0";
		
	}

	@Override
	public String exec(boolean notice, OAFlowInstanceWithBLOBs obj) throws Exception {
		final Map<String,Object> resultMap = new HashMap<>();
		String stampListId= "fd_35c783c0fab864";
		String compId = "fd_359789606b2e70";
		String stampsId = "fd_35a5c217a7dd24";
		String newKeeperId = "fd_358656a834da34";
		String flagId = "fd_35952f41263698";
		
		StringBuilder sb = new StringBuilder();
		String xmlStr = obj.getExtendDataXml();
//		System.out.println(xmlStr);
		XmlUtil.parseXmlWithHandler(xmlStr, "/java/object/void",new XMLHandler() {
			
			@Override
			public void hanle(Node paramNode) throws Exception {
				Node ele1 = paramNode.selectSingleNode("string");
				
				if(ele1.getText().equals(stampListId)) {
					List<Node> stampListNodes = paramNode.selectNodes("object");
					ArrayList<String> companyList = new ArrayList<String>();
					ArrayList<String> stampTypeList = new ArrayList<String>();
					
					for(Node stampListNode:stampListNodes) {
						List<Node> stampEleNodes = stampListNode.selectNodes("void/object/void");
						for(Node stampEleNode:stampEleNodes) {
							Node stampEleKeyNode = stampEleNode.selectSingleNode("string");
							if(stampEleKeyNode.getText().equals(compId)) {
								Node stampEleValNode = stampEleNode.selectSingleNode("string[2]");
								companyList.add(stampEleValNode.getText());
							}else if(stampEleKeyNode.getText().equals(stampsId)) {
								Node stampEleValNode = stampEleNode.selectSingleNode("string[2]");
								stampTypeList.add(stampEleValNode.getText());
							}
							
						}
						
					}
					resultMap.put("companyList", companyList);
					resultMap.put("stampTypeList", stampTypeList);
				}else if(ele1.getText().equals(newKeeperId)) {
					Node keeperIdNode = paramNode.selectSingleNode("object/void/string[2]");
					Node keeperNameNode = paramNode.selectSingleNode("object/void[2]/string[2]");
					resultMap.put("newKeeperId", keeperIdNode.getText());
					resultMap.put("newKeeperName", keeperNameNode.getText());
				}else if(ele1.getText().equals(flagId)) {
					Node flagNode = paramNode.selectSingleNode("string[2]");
					resultMap.put("flag", flagNode.getText().equals("0")?true:false);
				}
				
			}
		});
		
		String newKeeperObjId =  resultMap.get("newKeeperId").toString();
		String newKeeperName =  resultMap.get("newKeeperName").toString();
		List<String> companyList = (List<String>) resultMap.get("companyList");
		List<String> stampTypeList = (List<String>) resultMap.get("stampTypeList");
		boolean flag = (boolean) resultMap.get("flag");
		
		if(!flag) {  //是否为长期交接
			return null;
		}
		Person person = this.personDAO.selectByPrimaryKey(newKeeperObjId);
		String keeperLogname = person.getFdLoginName();
		
		for(int i=0;i<companyList.size();i++) {
			String companyName = companyList.get(i);
			String stampName = stampTypeList.get(i);
			
			Example exa_stamp = new Example(Stamp.class);
			exa_stamp.createCriteria().andEqualTo("company", companyName).andEqualTo("stampType", stampName).andEqualTo("state", (short)1);
			Stamp stamp = this.stampDAO.selectOneByExample(exa_stamp);
			if(stamp!=null) {
				if(!keeperLogname.equals(stamp.getKeeperLogname())) {//印章责任人变化
					stamp.setKeeperLogname(keeperLogname);
					stamp.setKeeperCname(newKeeperName);
					this.stampDAO.updateByPrimaryKeySelective(stamp);
					sb.append("印章:["+companyName+"-"+stampName+"]交接给"+newKeeperName+"\r\n");
				}
			}
		}
	
		return sb.toString();
	}

	
}
